package cn.edu.nju.software.action.admin;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.bean.show.AdminShow;
import cn.edu.nju.software.service.intf.AdminService;
import cn.edu.nju.software.util.Constants;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ModifyAdminPasswordAction extends ActionSupport implements SessionAware {

	private String oldPassword;
	private String newPassword;
	private AdminShow admin;
	private AdminService adminService;
	private Map<String, Object> session;

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	@Override
	public void validate() {
		this.clearFieldErrors();
		admin = (AdminShow) session.get(Constants.SESSION_ADMIN);
		if (!admin.getPassword().equals(oldPassword)) {
			this.addFieldError("ModifyAdminPasswordAction", "原密码错误！");
		}
	}

	@Override
	public String execute() throws Exception {
		admin.setPassword(newPassword);
		adminService.modifyAdminInfo(admin.getAdmin());
		return SUCCESS;
	}

}
